'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/agency.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';
import util from '../../components/util/util.js';

let pageEntry = (function() {
	/**
	 * 验证考生名称
	 * @param exam {string} 机构名称
	 */
	let verifyStudent = (studentName) => {
		//验证机构名称是否为空
		if (!studentName) {
			$(".form-error").empty();
			$(".form-input input").css("borderColor","#e7e7e7");
			$(".studentName").empty().html("*考生姓名不能为空");
			$("#studentName").css("borderColor","rgb( 255, 141, 50 )");
			return;
		}
		return true;
	}
	/**
	 * 验证准考证号
	 * @param exam {string} 机构名称
	 */
	let verifyExam = (examCode) => {
		//验证准考证号是否为空
		if (!examCode) {
			$(".form-error").empty();
			$(".form-input input").css("borderColor","#e7e7e7");
			$(".examCode").empty().html("*准考证号不能为空");
			$("#examCode").css("borderColor","rgb( 255, 141, 50 )");
			return;
		}
		return true;
	}
	return {
		initCheck() {
			let nowUrl = window.location.href;
			util.initLogin(nowUrl,"2");
		},
		setWindowHeight() {
			// 获取视窗高度，将背景图铺满屏幕
			let wh = (window.innerHeight > 0) ? window.innerHeight : screen.height;
			$('.page-apply-form-panel').height(wh);
		},
		bindEvent() {
			// 开始考试按钮事件
			$('#J_start_exam').off("click").on('click', function() {
				let studentName = $("#studentName").val();
				let examCode = $("#examCode").val();
				localStorage.setItem('userName', studentName);

				if(verifyStudent(studentName) && verifyExam(examCode)){
					$(".form-error").empty();
					$(".form-input input").css("borderColor","#e7e7e7");
					api.getExamInfo($.cookie('userCode'), studentName, examCode).then((resp) => {
						let examType = 0;

						if (resp.ret == '0') {
							// 获得已完成题数
							let currentExam = resp.content.paper.examQuestionIndex;
							if(currentExam == 7){
								$(".examCode").htlm("您已经考试完毕，请等待考试结果");
							}
							// 获得应该进入题目的模板
							examType = resp.content.paper.questions[currentExam].answerWay;

							if (examType == 1) {//音频
								window.location.href = '/examAudio.html?examCode=' + examCode;
							}
							else if (examType == 2) {//视频
								window.location.href = '/examVideo.html?examCode=' + examCode;
							}
						}
					});
				}
			});
		}
	}
})();

pageEntry.initCheck();
pageEntry.setWindowHeight();
pageEntry.bindEvent();
